package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.das.mechanic_base.R;

import java.util.List;

/**
 * 作者：zhanglei on 2019/11/1 12:36
 * <p>
 * 邮箱：916256628@qq.com
 */
public class X3GridViewIdeaAdapter extends BaseAdapter {

    private List<String> list;

    private Context context;

    private LayoutInflater layoutInflater;

    private int selection = -1;

    public X3GridViewIdeaAdapter(List<String> list, Context context) {

        this.list = list;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (viewHolder == null) {
            convertView = layoutInflater.inflate(R.layout.x3_gv_idea_item_adapter, null);
            viewHolder = new ViewHolder();
            viewHolder.text = convertView.findViewById(R.id.tv_title);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.text.setText(list.get(position));

        if (selection == position) {
            viewHolder.text.setBackgroundResource(R.drawable.x3_shape_idea_select_item);
            viewHolder.text.setTextColor(Color.parseColor("#0077FF"));
        } else {
            viewHolder.text.setBackgroundResource(R.drawable.x3_shape_idea_unselect_item);
            viewHolder.text.setTextColor(Color.parseColor("#333333"));
        }

        return convertView;
    }

    class ViewHolder {
        TextView text;
    }

    public void changeStatus(int pos) {
        selection = pos;
        notifyDataSetChanged();
    }

    public void changeSelectStatus(String select) {
        for (int i = 0; i < list.size(); i++) {
            if (select.equals(list.get(i))) {
                selection = i;
            }
        }
        notifyDataSetChanged();
    }

}
